package cn.element.leetcode.common;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: gonghongwei
 * @create: 2021-11-29 15:40
 */
public class RemoveDuplicates {


	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("123abccd");
		list.add("abcdc123");
		list.add("1234abc");
		list.add("ab1234cd");
		List list1 = removeSameChar(list);
		list1.forEach(e -> System.out.println(e));
	}


	public static List removeSameChar(List<String> lists) {
		Set<String> set = new HashSet<>();
		//1.遍历list取出数字,放到set集合中,放不进去表示重复移除即可
		Iterator iterator = lists.iterator();
		while (iterator.hasNext()) {
			String str = (String) iterator.next();
			String number = getNumber(str);
			if (!set.add(number)) {
				iterator.remove();
			}
		}
			return lists;
	}


	public static String getNumber(String s) {
		//从字符串中提取数字
		String regEx = "[^0-9]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(s);
		return m.replaceAll("").trim();
	}
}
